#coding:utf8
from models.models import Job
import smtplib
from email import base64mime

def Svae_Ontime_Job(authInfo, fromWho, ToMailList, MacroList, mainIndex,\
                    MailTitle, MailContext, Sdatetime, gdataInfo, AttachFilePath ,isSendMyself):
    '''
    参数与Send_Group_Mail()函数一致，最后多一个发送时间,
    返回字符串
    '''
    smtp = smtplib.SMTP()
    try:  #先测试密码是否正确，再保存
        smtp.connect(authInfo.get('server'))
        smtp.login(authInfo.get('user'), authInfo.get('password'))
    except Exception, e:
        print e
        return u"登录SMTP服务器失败，请检查服务器地址，用户名，密码的正确性！ %s" % e 
    smtp.quit()

    
    job = Job(smtp_server = authInfo.get('server'), login_user = authInfo.get('user'), \
              login_psw = base64mime.encode(authInfo.get('password')),\
              sender_mail = fromWho, mail_list = ToMailList, marco = MacroList, \
              subject = MailTitle, mail_context = MailContext, \
              user_id = 0, send_date = Sdatetime, attach_file_path = AttachFilePath, \
              glogin_mail = gdataInfo['gLogMail'], gpassword = base64mime.encode(gdataInfo['gPsw']), \
              gfile_name = gdataInfo['gFileName'], gwksht_name = gdataInfo['wksht_name'],\
               send_myself = isSendMyself, main_index = mainIndex)
                          #temp_name 在job表中保存为附件路径

    job.save() #把内容保存到数据库中

    return "保存定时任务成功！邮件将在稍后时间内发送，并通过邮件告知你结果 。"
#end format_from_locad